package com.sell.contorller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import com.sell.entity.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.sell.service.*;
import com.sell.util.Pager;

@Controller
@RequestMapping("/sales")
public class SalesorderController {
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ");  
	@Resource
	private SalesorderService salesorderService;
    @Resource
    private EmployeesService employeesService;
    @Resource
    private MembersService memberService;
    @Resource
    private ProductService productService;
    @Resource
    private MembertypeService membertypeService;

	/**
	 * 销售记录查询
	 */
	@RequestMapping(value = "/saleslist.html", method = RequestMethod.GET)
	public String showSaleslist(Model model, @RequestParam(value = "pageNo", required = false) String pageNoStr,
			@RequestParam(value = "sales", required = false) String sales, HttpSession session) {
		Employees emp = (Employees) session.getAttribute("employees");
		Integer eshopcode = emp.getEshopcode();
		int a = 2;
		if (sales == null) {
			a = 1;
		} else {
			a = Integer.parseInt(sales);
		}
		model.addAttribute("sales",a);
		int pageNo = 1;
		if (pageNoStr != null && !pageNoStr.equals("")) {
			pageNo = Integer.parseInt(pageNoStr);
		}
		try {
			Pager pager = new Pager(salesorderService.salesnum(eshopcode), 7, pageNo);
			List<Salesorder> saleslist = salesorderService.findAllSalesorder(a, eshopcode, pager);
			for(Salesorder sa:saleslist){
				Product pro=productService.getProductByid(sa.getProductid());
				sa.setProduct(pro);
				Employees emp2=employeesService.getEmpById(sa.getEmployeesid());
				sa.setEmployees(emp2);
				if(sa.getMembersid()!=null){
				Members member=memberService.getMemberByid(sa.getMembersid());
				sa.setMembers(member);
				}else{
					sa.setMembers(null);
				}
			}
			model.addAttribute("saleslist", saleslist);
			model.addAttribute("pager",pager);
			model.addAttribute("sales",a);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "salesorder";
	}
	/**
	 * 跳转到添加页面
	 * @return
	 */
	@RequestMapping(value = "/salesadd.html")
	public String Showsalesadd() {
		return "salesadd";
	}
	/**
	 * 保存数据
	 * @return
	 */
	@RequestMapping(value = "/salesave.html",method = RequestMethod.POST)
	public String salesave(String productid,String saleCount,String salesPrice,String saleDate,
			@RequestParam(value = "cardid",required = false)String cardid,String employeesid,
			HttpSession session,Model model) {
        try {
        	double money=0;
        	Salesorder sales=new Salesorder();
			int pro=Integer.parseInt(productid);
			int salecount=Integer.parseInt(saleCount);
			double salesmoney=Double.parseDouble(salesPrice);
			if(cardid!=""){
				Members member=memberService.getBycardid(cardid);
				Membertype memtype=membertypeService.getMemTypeById(member.getTypeid());
				money=salesmoney*memtype.getDiscount();
				int integral=(int)money; //积分
				sales.setMembersid(member.getMembersid());
				sales.setSalesprice(money);
				memberService.updateMember(money, integral, member.getMembersid());
				Members member2=memberService.getMemberByid(member.getMembersid());
				if(member2.getTotalmoney()>500){
					memberService.updatetypeid(2, member2.getMembersid());
				}else if(member2.getTotalmoney()>1000){
					memberService.updatetypeid(3, member2.getMembersid());
				}else if(member2.getTotalmoney()>2000){
					memberService.updatetypeid(4, member2.getMembersid());
				}
			}else{
				sales.setMembers(null);
				sales.setSalesprice(salesmoney);
			}
			int emp=Integer.parseInt(employeesid);
			sales.setProductid(pro);
			sales.setSalecount(salecount);
			sales.setSaledate(new Date());
			sales.setEmployeesid(emp);
			Employees employ = (Employees)session.getAttribute("employees");
			sales.setEshopcode(employ.getEshopcode());
			int num=salesorderService.addSalesorder(sales);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "salesadd";
		}
		return "redirect:/sales/saleslist.html";
	}
	
}
